package cn.nawang.ebeim.ui.controller;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.nawang.ebeim.client.version.DirectoryNode;
import cn.nawang.ebeim.client.version.Node;
import cn.nawang.ebeim.client.version.VersionTree;
import cn.nawang.ebeim.client.version.VersionTreeUtil;
import cn.nawang.ebeim.ui.bean.BackupLogWrapper;
import cn.nawang.ebeim.ui.util.ApiUtil;
import cn.nawang.ebeim.ui.util.UIversionTreeUtil;

/**
 * @ClassName: BackLogController
 * @Description: TODO
 * @author fanyb
 * @date Nov 12, 2015 5:39:10 PM
 * @version v1.0
 */
@RestController
public class BackLogController {
  private static final Logger LOG = LoggerFactory.getLogger(BackLogController.class);
  
  /**
   * @author zhangjf
   * @Description: 获取备份日志列表
   * @param @param backupLogWrapper
   * @param @return
   * @return String
   * @date 2015年10月29日上午8:43:35
   *
   * @修改人 zhangjf
   * @修改日期
   */
  @RequestMapping(value = "/backlog/list", method = RequestMethod.POST)
  public String getBackupLog(@RequestBody BackupLogWrapper backupLogWrapper) {
    LOG.info("dsId - {}", backupLogWrapper);
    boolean isRoot = false;
    if(backupLogWrapper.getBean()!=null){
      String path = backupLogWrapper.getBean().getPath();
      String dsId = backupLogWrapper.getBackupLog().getDsId();
      int versionMajor = backupLogWrapper.getBackupLog().getVersionMajor();
      VersionTree tree = VersionTreeUtil.loadVersionTree(dsId, versionMajor+".0");
      Node node = tree.findNode(path);
      String parentDsid = UIversionTreeUtil.getParentDsId(node.getParent());
      if(node instanceof DirectoryNode && ((DirectoryNode)node).getDsId()!=null){
        backupLogWrapper.getBackupLog().setDsId(((DirectoryNode)node).getDsId());
        isRoot = true;
      }else if(parentDsid!=null){
        backupLogWrapper.getBackupLog().setDsId(parentDsid);
      }else{
        DirectoryNode dn = (DirectoryNode)node;
        if(dn.isHandAdd()){
          String backuplogs = getHandAddFolderLogs(dn,dsId);
          return backuplogs;
        }
      }
    }
    
    String result = ApiUtil.getBackupLog(backupLogWrapper);
    JSONObject json = JSONObject.parseObject(result);
    json.put("isRoot", isRoot);
    LOG.debug("getBackupLog: {}", result);
    return json.toString();
  }
  
  public String getHandAddFolderLogs(DirectoryNode dn,String dsId){
    JSONObject json = new JSONObject();
    json.put("code", 200);
    JSONArray jsonArr = new JSONArray();
    JSONObject log = new JSONObject();
    log.put("id", UUID.randomUUID().toString());
    log.put("dsId",dsId);
    log.put("status", 0);
    log.put("versionMajor", 1);
    log.put("versionMinor", 0);
    log.put("remark", "手动新增文件夹");
    log.put("beginTime", dn.getCreateTime());
    log.put("endTime", dn.getCreateTime());
    
    jsonArr.add(log);
    
    JSONObject objList = new JSONObject();
    objList.put("objList", jsonArr);
    
    json.put("list", objList);
    return json.toString();
  }
  
}
